package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// SetPromotionItemsParam 设置多条促销活动商品配置
type SetPromotionItemsParam struct {
	PromotionPtr *sqlc.Promotion
	ItemConfPtrs param.ItemConfigurationPtrArray
}

// SetPromotionItems 设置促销活动商品配置
func (s *PromotionEntityServiceImp) SetPromotionItems(ctx context.Context, q *sqlc.Queries, ptr *SetPromotionItemsParam) (err error) {
	promotionID := ptr.PromotionPtr.ID
	err = q.DisablePromotionItemsByPromotionID(ctx, promotionID)
	if err != nil {
		return
	}
	if len(ptr.ItemConfPtrs) > 0 {
		for idx, itemConf := range ptr.ItemConfPtrs {
			err = q.UpsertPromotionItem(ctx, sqlc.UpsertPromotionItemParams{
				PromotionID: promotionID,
				ItemID:      itemConf.ID,
				ItemName:    itemConf.Name,
				SortNo:      int32(idx + 1),
			})
			if err != nil {
				return
			}
		}
	}
	return
}
